#include "Video.h"
#include "VideoUI.h"
#include "player.h"
#include <unistd.h>
#include <dirent.h>
#include "sys.h"

#define VIDEO_DIR "/overlay/videos/"


// 支持的视频文件格式
static const char *fileType[] =
    {
        ".avi", ".mkv", ".flv", ".ts", ".mp4", ".webm", "asf", "mpg", ".mpeg", ".mov", ".vob", ".3gp", ".wmv", ".pmp"};

/**
 *@brief Video构造函数
 *@param exit_cb 退出进程回调函数
 */
Video::Video(function<void(void)> exitCb, mutex &UIMutex)
{
    threadExitFlag = false;
    uiMutex = &UIMutex;

    // 设置UI回调函数
    VideoUI::Operations uiOpts = {0};
    uiOpts.exitCb = exitCb;
    uiOpts.getStateCb = std::bind(&Video::getState, this);
    uiOpts.getVolumeCb = std::bind(&Video::getVolume, this);
    uiOpts.pauseCb = std::bind(&Video::pause, this);
    uiOpts.playCb = std::bind(&Video::play, this, std::placeholders::_1);
    uiOpts.setCurCb = std::bind(&Video::setCur, this, std::placeholders::_1);
    uiOpts.setVolumeCb = std::bind(&Video::setVolume, this, std::placeholders::_1);

    VideoUI::create(uiOpts);

    pThread = new thread(&Video::threadFunction, this); // 创建执行线程
}

Video::~Video()
{
    threadExitFlag = true;
    pThread->join();
    delete pThread;

    VideoUI::release();
}

/**
 *@brief 搜索某个目录下的视频文件,支持多种格式
 *@param path 目录路径
 *@return 搜索到的音乐个数
 */
int Video::searchVideo(string path)
{
    int cnt = 0;
    int i;
    bool legalVideo = false;
    std::string filePath;

    struct dirent *ent;
    DIR *dir = opendir(path.c_str());

    for (i = 0;; i++)
    {
        ent = readdir(dir);
        if (ent == NULL)
            break;

        if (ent->d_type == DT_REG)
        {
            const char *pfile = strrchr(ent->d_name, '.');
            if (pfile != NULL)
            {
                filePath = path + std::string(ent->d_name);

                for (int j = 0; j < sizeof(fileType) / sizeof(fileType[0]); j++)
                {
                    if (strcasecmp(pfile, fileType[j]) == 0)
                    {
                        printf("%s file\n", fileType[j]);
                        legalVideo = true;
                        break;
                    }
                }
            }
        }
        if (legalVideo == true)
        {
            legalVideo = false;
            uiMutex->lock();
            VideoUI::addVideoList(ent->d_name);
            uiMutex->unlock();

            cnt++;
        }

        usleep(50000);
    }

    closedir(dir);

    return cnt;
}

int Video::threadFunction(void)
{
    usleep(50000);

    gMplayer = new MediaPlayer(); // 创建播放器

    searchVideo(VIDEO_DIR);

    while (!threadExitFlag)
    {
        int cur = gMplayer->GetCurrent();
        int total = gMplayer->GetDuration();

        uiMutex->lock();
        VideoUI::setPlayProgress(cur / 1000, total / 1000);
        uiMutex->unlock();

        usleep(50000);
    }

    delete gMplayer;

    return 0;
}

/**
 * @brief UI获取视频播放状态回调函数
 */
bool Video::getState(void)
{
    bool state = false;

    if (gMplayer != nullptr)
        state = gMplayer->GetState();

    return state;
}

/**
 * @brief UI获取音量回调函数
 */
int Video::getVolume(void)
{
    int volume = 0;

    if (gMplayer != nullptr)
        volume = gMplayer->GetVolume();

    return volume;
}

/**
 * @brief UI暂停视频回调函数
 */
void Video::pause(void)
{
    if (gMplayer != nullptr)
        gMplayer->Pause();
}

/**
 * @brief UI播放视频回调函数
 */
void Video::play(const char *name)
{
    if (name == NULL)
    {
        gMplayer->Start(); // 继续播放
        return;
    }

    // 播放新的视频
    string url = VIDEO_DIR + string(name);
    gMplayer->PlayNewVideo(url);
    gMplayer->Start();
}

/**
 * @brief UI设置播放时间点回调函数
 */
void Video::setCur(int cur)
{
    if (gMplayer != nullptr)
        gMplayer->SetCurrent(cur * 1000);
}

/**
 * @brief UI设置音量回调函数
 */
void Video::setVolume(int volume)
{
    if (gMplayer != nullptr)
        gMplayer->SetVolume(volume);
}
